<?php
namespace Mobile\Controller;

use Think\Controller;

class OrderController extends Controller
{
    public function index()
    {
        //生产订单所需要的数据
        $_SESSION['mobile']['data'];
        //放映时间
        $time2= $_SESSION['mobile']['data']['ftime'];
        $timeday =time();  //当前时间搓
        $time4= date('Ymd',$time2 )-date('Ymd',$timeday );
        //播放日期
        $time11=date('(m月d日) H:i',$time2);
        if($time4==0){
            $this->assign('message','今天');
            $this->assign('time11',$time11);
            $this->assign('language',$_SESSION['mobile']['data']['yy']);//语言
        }else if($time4==1){
            $this->assign('message','明天');
            $this->assign('time11',$time11);
            $this->assign('language',$_SESSION['mobile']['data']['yy']);//语言
        }else if($time4==2){
            $this->assign('message','后天');
            $this->assign('time11',$time11);
            $this->assign('language',$_SESSION['mobile']['data']['yy']);//语言
        }
        //电影的名字
        $movieid = $_SESSION['mobile']['mid'];
        $movielist = M('movie')->where('id='.$movieid)->find();
        $_SESSION['mobile']['data']['moviename']=$movielist['name'];
        //发送电影的名字
        $this->assign('moviename', $_SESSION['mobile']['data']['moviename']);
        //发送座位的数量
        $this->assign('total_fee', $_SESSION['mobile']['data']['total_fee']);
        //exit;
        $item = M('order_item');
        $list=  $item ->where('id='. $_SESSION['mobile']['data']['orderitrm'])->find();
        foreach($list as $key=>$val){
            //处理座位
            $row = explode(',', $list['seat_row']);//行
            $col = explode(',', $list['seat_column']);//列
        }
        for ($i = 0; $i < count($row); $i++) {
            $seat[$i]['row'] = $row[$i];
            $seat[$i]['col'] = $col[$i];
        }
        $this->assign('seat', $seat);  // 座位
        $pay = time() - $list[0]['created_at']; //支付时间状态
        //获取订单的id
        $orderId = $list['order_id'];
        $order =M('order');
        $orderlist =$order->where('id='.$orderId)->find();
        //获取订单生成的时间
        $orderTime=$orderlist['created_at'];
        $nowTime =time()- $orderTime;
        if($nowTime >=600){
                //如果超过10分钟就禁用订单
            $status = $order->save('status=30');
            $this -> assign('status','该订单已失效请重新下单');
        }
//        var_dump($_SESSION['mobile']['data']['sid']);
        $this->display();
    }

    // ajax提交数据的地方
    public function ajax()
    {
        //生产订单所需要的数据
        $_SESSION['mobile']['data'];
        //把ajax提及哦啊过来的数据存入数据库
        $model = M('order'); //订单表
        $data = $_POST;
        $srts = $data['seat']; //座位
        $arr1 = [];
        //遍历座位号  行也列
        foreach ($srts as $key => $val) {
            //在把 每一个最为都分成 一个小的数组
            $arr1[$key]['row'] = explode(',', $val)[0]; //下标为0的  行
            $arr1[$key]['col'] = explode(',', $val)[1]; //下标为1的  列
        }
        $count=count($arr1);//获取座位的个数 也就是 电影票的数量
        $data['user_id'] = $_SESSION['mobile']['uid'];  //影院id
        $data['member_id'] = $_SESSION['mobile']['id'];//会员id
        $data['movie_id'] = $_SESSION['mobile']['mid'];//电影id
        $data['screening_id'] =$_POST['sid'];//场次id
        $data['price'] = $_SESSION['mobile']['data']['price'];//单价
        $data['quantity'] = $count;//数量 等于座位的数量
        $data['total_fee'] = $data['quantity'] * $data['price'];//总金额
        $_SESSION['mobile']['data']['total_fee'] =  $data['quantity'] * $data['price'];//session 里面也存一份
        $data['status'] = 10;//状态 进行中
        $data['payment_status'] = 10;//支付状态 未付款
        $data['created_at'] = time();//新增时间
        $list = $model->data($data)->add(); //产出订单的id
        if ($list) {
            //订单项 表order_id
            $orderItem = M('OrderItem');
            $row = '';
            $col = '';
            foreach ($arr1 as $ke => $va) {
                $row .= $va['row'] . ',';
                $col .= $va['col'] . ',';
            }
            $dat['screening_id'] =$_POST['sid'];//场次id
            $dat['seat_row'] = trim($row, ',');  //座位行
            $dat['seat_column'] = trim($col, ',');//座位列
            $dat['created_at'] = time(); //添加时间
            $dat['order_id'] =  $list; //订单的id
            $list1 = $orderItem->data($dat)->add();
            $_SESSION['mobile']['data']['orderitrm']=$list1;//订单项的id
            if ($list1) {
                echo 1;
            } else {
                echo 0;
            }
        }

    }












}